
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.Managers;
using Tathata.Common.Utils;
using System.Collections.Generic;
using Tathata.Common.BLL.EntityCollections;

namespace Tathata.Web.Administration
{
    public partial class Administration_CampaignDetails : BaseNopAdministrationPage
    {
        private void BindData()
        {
            Campaign campaign = CampaignManager.GetByCampaignID(this.CampaignID);
            if (campaign != null)
            {
                this.txtName.Text = campaign.Name;
                this.txtSubject.Text = campaign.Subject;
                this.txtBody.Value = campaign.Body;
                this.lblCreatedOn.Text = campaign.CreatedOn.ToString();
            }
            else
                Response.Redirect("Campaigns.aspx");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.BindData();
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    Campaign campaign = CampaignManager.GetByCampaignID(this.CampaignID);

                    if (campaign != null)
                    {
                        campaign = CampaignManager.UpdateCampaign(campaign.CampaignID,
                       txtName.Text, txtSubject.Text, txtBody.Value, campaign.CreatedOn);
                        Response.Redirect("CampaignDetails.aspx?CampaignID=" + campaign.CampaignID.ToString());
                    }
                    else
                        Response.Redirect("Campaigns.aspx");
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            try
            {
                CampaignManager.DeleteCampaign(this.CampaignID);
                Response.Redirect("Campaigns.aspx");
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }

        protected void btnSendTestEmail_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    CampaignManager.SendCampaign(this.CampaignID, txtSendTestEmailTo.Text);
                    lblSendEmailResult.Text = "Email has been successfully sent.";
                }
                catch (Exception exc)
                {
                    lblSendEmailResult.Text = exc.Message;
                }
            }
        }


        protected void btnSendMassEmail_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                int scriptTimeout = base.Server.ScriptTimeout;
                try
                {
                    Server.ScriptTimeout = 300;
                    CustomerCollection customers = CustomerManager.GetAllCustomers();
                    int totalEmailsSent = CampaignManager.SendCampaign(this.CampaignID, customers);
                    lblSendEmailResult.Text = string.Format("Emails has been successfully sent to {0} customers.", totalEmailsSent);
                }
                catch (Exception exc)
                {
                    lblSendEmailResult.Text = exc.ToString();
                }
                finally
                {
                    Server.ScriptTimeout = scriptTimeout;
                }
            }
        }
        public int CampaignID
        {
            get
            {
                return CommonHelper.QueryStringInt("CampaignID");
            }
        }
    }
}
